const cheerio = require('cheerio');
const _ = require('underscore');
const Base = require('./_base')
const path = window.require('path');
const md5 = require('js-md5');

export default class Gather extends Base{
  constructor(reportCb) {
    super(reportCb);
    this.enterurl = 'http://angelocappellini.com/'
    this.pathname = path.resolve('data/' + md5(this.enterurl))
  }
  getProductUrls = async() => {
    let res = await fetch(this.enterurl).then(res => res.text());
    let $ = cheerio.load(res)
    if($("#main_mega_menu ul").length<1)return false;
      let html = $("#main_mega_menu ul").eq(0).html();
    html = $('li',html).eq(1).html();
    let sites =[]
    $('li',html).each(function(i,e){
      if($('a',$(this).html()).attr('href')===undefined)return false;
      sites.push($('a',$(this).html()).attr('href'));
    });
    let rurls = []
      console.log("开始获取 列表页")
      for (let key of sites) {
        let res=await fetch(key).then(res => res.text());

        let $ = cheerio.load(res)
        // 获取商品的详细地址
        var product={};
        let goods_path_list=[];
        let goods_list=$('.dfd-portfolio-fitRows').html();
        $('.project',goods_list).each(function(i,e){
          if($('a',$(this).html())===undefined)return false;
          product.name=$('a',$(this).html()).text();
          rurls.push($('a',$(this).html()).attr('href'));
        });
      }
    return rurls
  }
  getProduct = async(url) => {
    console.log('fetch product', url)
    let res = await fetch(url).then(res => res.text());
    let $ = cheerio.load(res)
    // 获取商品的详细地址
    let product={}
    product.origin_html=$('html').html();
    product.origin_url = url;
    product.brand_num='angelocappellini';
    product.images=[];
    product.path=[];
    product.local_path=[];
    product.date=new Date().getTime();
    product.description=$('.wpb_text_column p').text();
    if(('.wpb_single_image').length<1)return false;
    $('.wpb_single_image').each(function(j,el){
      let img=$('img',$(this).html()).attr('src');
      product.images.push({"pic_url":img});
    });
    return product
  }
}
